package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.daoimpl.ManagerAppDaoImp;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.ManagerApp;
import com.zretc.emptraining.service.CourseService;
import com.zretc.emptraining.service.ManagerAppService;
import com.zretc.emptraining.serviceimpl.CourseServiceImpl;
import com.zretc.emptraining.serviceimpl.ManagerAppServiceImp;

/**
 * Servlet implementation class ManagerAppServlet
 */
@WebServlet("/ManagerAppServlet")
public class ManagerAppServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
//	ManagerAppDaoImp madi = new ManagerAppDaoImp();
	ManagerAppService madi = new ManagerAppServiceImp();
	CourseService cs = new CourseServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManagerAppServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset = utf-8");
		String op = "";
		String keyword = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if (op.equals("showManagerAppByPage")) {
			// 分页查询 -- 多种情况查询
			showManagerAppByPage(request, response, keyword);
		} else if (op.equals("delManagerApp")) {
			// 管理员删除
			delManagerAppByAdmin(request, response);
		} else if (op.equals("passManagerApp")) {
			// 管理员通过申请
			passManagerApp(request, response);
		} else if (op.equals("refuseManagerApp")) {
			// 管理员不通过
			refuseManagerApp(request, response);
		} else if (op.equals("addManagerApp")) {
			// 添加
			addManagerApp(request, response);
		} else if (op.equals("updataManagerApp")) {
			// 修改信息
			updateManagerApp(request, response);
		} else if (op.equals("delAll")) {
			// 批量删除
			delAll(request, response);
		} else if (op.equals("showManagerAppByManager")) {
			//主管-部门课程申请
			doShowManagerAppByManager(request, response);
		}

	}

	private void doShowManagerAppByManager(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 这里是从前台获取的部门号的数据
		// String deptNo = request.getParameter("deptNo");
		// 我现在先拿个模拟数据先来演示

		int deptNo = Integer.parseInt(request.getParameter("deptNo"));
		int page = 1;
		int pageSize = 10;
		String deptName = request.getParameter("deptName");
		System.out.println("deptName:" + deptName);
		if (deptName == null || deptName.equals("")) {
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<ManagerApp> pd = new PageData<ManagerApp>();
			//
			pd = madi.getManagerAppByDeptNo(page, pageSize, deptNo);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("course-add.jsp").forward(request, response);
		} else {
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<ManagerApp> pd = new PageData<ManagerApp>();
			pd = madi.getMnagerAppByDeptNoWithDeptName(page, pageSize, deptNo, deptName);
			request.setAttribute("pd", pd);
			request.setAttribute("deptName", deptName);
			request.getRequestDispatcher("course-add.jsp").forward(request, response);
		}
	}

	private void showManagerAppByPage(HttpServletRequest request, HttpServletResponse response, String keyword)
			throws ServletException, IOException {
		String select = "";
		if (request.getParameter("select") != null) {
			select = request.getParameter("select");
		}
		if (select.equals("") || keyword.equals("")) {
			System.out.println("3");
			// 查询所有，可删除！！
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<ManagerApp> pd = new PageData<ManagerApp>();
			pd = madi.getManagerAppsByLike(page, pageSize, "");
			System.out.println(pd.getData());
			request.setAttribute("select", select);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("ManagerAppList.jsp").forward(request, response);
		} // 根据课程名进行模糊查询
		else if (select.equals("课程名")) {
			System.out.println("1");
			if (request.getParameter("key") != null) {
				keyword = request.getParameter("key");
			}
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<ManagerApp> pd = new PageData<ManagerApp>();
			pd = madi.getManagerAppsByLike(page, pageSize, keyword);
			request.setAttribute("key", keyword);
			request.setAttribute("select", select);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("ManagerAppList.jsp").forward(request, response);
		} else if (select.equals("部门号")) {
			System.out.println("2");
			// 根据部门精准查询
			int key = Integer.parseInt(request.getParameter("key"));
			int page = 1;
			int pageSize = 10;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			PageData<ManagerApp> pd = new PageData<ManagerApp>();
			pd = madi.getManagerAppByDeptNo(page, pageSize, key);
			request.setAttribute("key", key);
			request.setAttribute("select", select);
			request.setAttribute("pd", pd);
			request.getRequestDispatcher("ManagerAppList.jsp").forward(request, response);
		}
	}

	/**
	 * 批量删除主管申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String deleteAll = request.getParameter("deleteAll");
		System.out.println("deleteall:" + deleteAll);
		deleteAll = deleteAll.substring(0, deleteAll.length() - 1);
		String[] strs = deleteAll.split(",");
		boolean flag = false;
		for (int i = 0; i < strs.length; i++) {
			flag = madi.delManagerAppByAdmin(Integer.parseInt(strs[i]));
		}
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	/**
	 * 修改主管申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void updateManagerApp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String deptno = request.getParameter("deptNo");
		int deptNo = Integer.parseInt(deptno);
		int managerappNo = Integer.parseInt(request.getParameter("managerappNo"));
		String applyReason = request.getParameter("applyReason");
		System.out.println("applyReason：" + applyReason);
		String deptName = request.getParameter("deptName");
		ManagerApp managerApp = new ManagerApp(managerappNo, deptNo, deptName, applyReason);
		boolean flag = madi.UpdateManagerApp(managerApp);
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	/**
	 * 主管申请添加
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void addManagerApp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int deptNo = 0;
		String deptName = "";
		String applyReason = "";
		if (request.getParameter("deptNo") != null) {
			deptNo = Integer.parseInt(request.getParameter("deptNo"));
		}
		
		if (request.getParameter("deptName") != null) {
			deptName = request.getParameter("deptName");
		}
		if (request.getParameter("applyReason") != null) {
			applyReason = request.getParameter("applyReason");
		}
		ManagerApp managerApp = new ManagerApp(deptNo, deptName, applyReason);
		boolean flag = madi.addManagerApp(managerApp);
		Gson gson = new Gson();
		String gsonString = gson.toJson(flag);
		System.out.println();
		PrintWriter printWriter = response.getWriter();
		printWriter.print(gsonString);
		printWriter.close();
	}

	/**
	 * 管理员通过后拒绝主管申请
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void refuseManagerApp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int managerappNo = Integer.parseInt(request.getParameter("managerappNo"));
		boolean flag = madi.refuseManagerApp(managerappNo);
//			System.out.println(flag);

		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
//			System.out.println(jsonString);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	/**
	 * 管理员通过主管申请课程
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void passManagerApp(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int managerappNo = 0;
		if (request.getParameter("managerappNo") != null) {
			managerappNo = Integer.parseInt(request.getParameter("managerappNo"));
		}
		boolean flag = madi.passManagerApp(managerappNo);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	/**
	 * 管理员单个删除信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delManagerAppByAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int managerappNo = Integer.parseInt(request.getParameter("managerappNo"));
		boolean flag = madi.delManagerAppByAdmin(managerappNo);
//			System.out.println(flag);
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
//			System.out.println(jsonString);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonString);
		printWriter.close();
	}

	/*
	 * private void showManagerApp(HttpServletRequest request, HttpServletResponse
	 * response, String keyword) throws ServletException, IOException { if
	 * (request.getParameter("deptname") != null) { keyword =
	 * request.getParameter("deptname"); }
	 * 
	 * int page = 1; int pageSize = 10; if (request.getParameter("page") != null) {
	 * page = Integer.parseInt(request.getParameter("page")); }
	 * 
	 * PageData<ManagerApp> pd = new PageData<ManagerApp>(); pd =
	 * madi.getManagerAppsByLike(page, pageSize, keyword);
	 * request.setAttribute("deptname", keyword); request.setAttribute("pd", pd);
	 * request.getRequestDispatcher("ManagerAppList.jsp").forward(request,
	 * response); }
	 */

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
